<?php

require_once __DIR__.'/../config.php';

class IMAGE
{
    public static function save()
    {
        $save_folder = ABSPATH . '/public/image/';

        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["file"]["name"]);

        $extension = end($temp);     // 获取文件后缀名

        if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/jpg")
            || ($_FILES["file"]["type"] == "image/pjpeg")
            || ($_FILES["file"]["type"] == "image/x-png")
            || ($_FILES["file"]["type"] == "image/png"))
            //&& ($_FILES["file"]["size"] < 204800)   // 小于 200 kb
            && in_array($extension, $allowedExts))
        {
            if ($_FILES["file"]["error"] > 0)
            {
                return false;
            }
            else
            {

                $time = date('YmdHis');
                $save_file = $time.'.'.$extension;

                move_uploaded_file($_FILES["file"]["tmp_name"], $save_folder.$save_file);

                return "/image/" . $save_file;
            }
        }
        else
        {
            return false;
        }
    }
}